// components/hot/hto.js
const tools = require("../../utils/tool")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isMore: {
      type: Boolean,
      value: false,
    },
    typ: {
      type: Number,
      value: 0
    },

    hot: {
      type: Array,
      observer: async function (newVal) {
        this.setData({
          hot1: newVal
        })
        const cloneVal = JSON.parse(JSON.stringify(newVal));
        setTimeout(() => {
          this.initObserver(cloneVal)
        }, 0);
      }
    }
  },
  data: {
    height: '',
    hot1: []
  },

  lifetimes: {

  },
  methods: {
    initObserver(cloneVal) {
      var that = this;
      return new Promise((resolve, reject) => {
        for (let i in cloneVal) {
          that.createIntersectionObserver().relativeToViewport({
            bottom: 20
          }).observe('.item-' + i, (ret) => {
            var show = 'hot1[' + i + '].show'
            if (ret.intersectionRatio > 0) {
              this.setData({
                [show]: true
              })
            }
          })
        }
      })
    },
    toDetail() {
      wx.navigateTo({
        url: '/pages/index/reduce/index',
      })
    },

    // 点赞

    bindgive(e) {
      console.log(e)
      let id = e.currentTarget.dataset.id //文章id
      let index = e.currentTarget.dataset.index
      this.triggerEvent('myevent', {
        id,
        index
      })
    },


    // 点赞
    bindGive(e) {
      let id = e.currentTarget.dataset.id //文章id
      let index = e.currentTarget.dataset.index
      let methods=this.data.hot1[index].praised?'delete':'post'
      let give = tools.request('/aty/applet/a/praise/' + id, methods, {}, wx.getStorageSync('token'))
      give.then(res => {
        if (res.statusCode==200 || res.statusCode==204) {
          if (this.data.hot[index].praised) {
            this.setData({
              [`hot1[${index}].praised`]: false,
              // [`hot1[${index}].click_num`]: this.data.hot1[index].click_num - 1
            })
            tools.showToast('取消点赞')
          } else {
            this.setData({
              [`hot1[${index}].praised`]: true,
              // [`hot1[${index}].click_num`]: this.data.hot1[index].click_num + 1
            })
            tools.showToast('点赞成功')
          }
        } else {
          tools.showToast(res.data.error_msg, 'error')
        }
      })
    },

    // 收藏
    bindCollect(e) {
      tools.showLoading()
      let id = e.currentTarget.dataset.id //文章id
      let index = e.currentTarget.dataset.index
      let methods=this.data.hot1[index].collected?'delete':'post'
      let collect = tools.request('/aty/applet/a/collect/' + id, methods, {}, wx.getStorageSync('token'))
      collect.then(res => {
        wx.hideLoading()
        if (res.statusCode==200 || res.statusCode==204) {
          if (this.data.hot[index].collected) {
            this.setData({
              [`hot1[${index}].collected`]: false,
              // [`hot1[${index}].collect_num`]: this.data.hot1[index].collect_num - 1
            })
            tools.showToast('取消收藏')
          } else {
            this.setData({
              [`hot1[${index}].collected`]: true,
              // [`hot1[${index}].collect_num`]: this.data.hot1[index].collect_num + 1
            })
            tools.showToast('收藏成功')
          }
        } else {
          tools.showToast(res.data.error_msg, 'error')
        }
      })
    },
    // 点击头像查看别人主页
    bindZhuYe(e) {
      console.log(e.currentTarget.dataset)
      wx.navigateTo({
        url: '/pages/mine/mine/mine?',
      })
    },
    share(e) {
      let id = e.currentTarget.dataset.id
      let index = e.currentTarget.dataset.index
      let share = tools.request('/api/applets/article/share/' + id, 'get', {}, wx.getStorageSync('token'))
      share.then(res => {
        if (res.data.status) {
          this.setData({
            [`hot1[${index}].share_num`]: this.data.hot[index].share_num + 1
          })
        }
      })
    }
  }
})